package tianxinyi.rbac.view;

import tianxinyi.rbac.domain.dto.UserDto;

import tianxinyi.rbac.domain.vo.DeptVo;
import tianxinyi.rbac.domain.vo.Result;
import tianxinyi.rbac.domain.vo.RoleVo;
import tianxinyi.rbac.domain.vo.UserVo;
import tianxinyi.rbac.service.IUserService;
import tianxinyi.rbac.service.array.UserService;


public class UserView {

    public static UserVo[] cache;

    private static final IUserService userService;

    static {
        userService = UserService.getInstance();
    }

    public static void show(){
        String menuChoice;
        do {
            printMenu();
            menuChoice = MainView.input.nextLine();
            switch (menuChoice) {
                case "0" -> System.out.println("已返回上一级菜单");
                case "1" -> list();
                case "2" -> create();
                case "3" -> update();
                case "4" -> toggle();
                default  -> System.out.println("输入非法，请输入正确的菜单编号！");
            }
        }while (!"0".equals(menuChoice));
    }

    public static void printMenu(){
        String menu = """
                -------------------
                      用户管理
                -------------------
                |   0...返回上级    |
                |   1...用户列表    |
                |   2...创建用户    |
                |   3...更新用户    |
                |   4...切换状态    |
                -------------------
                请输入选项：""";
        System.out.print(menu);
    }

    public static void printError(String message) {
        System.out.println("\033[31m" + message + "\033[0m");
    }

    public static void list() {
        Result result = userService.list();
        cache = (UserVo[]) result.data;
        System.out.println("ID\t用户名\t姓名\t手机号\t邮箱\t部门名称\t角色名称\t状态");

        for (UserVo user : cache) {
            System.out.printf("%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",user.id, user.username, user.name, user.mobile, user.email, user.deptId, user.roleId, user.enabled, user.deptName, user.roleName, user.enabled ? "启用" : "禁用");
        }
    }

    public static void create() {
        System.out.print("请输入用户名：");
        String username = MainView.input.nextLine();
        System.out.print("请输入姓名：");
        String name = MainView.input.nextLine();
        System.out.print("请输入手机号：");
        String mobile = MainView.input.nextLine();
        System.out.print("请输入邮箱：");
        String email = MainView.input.nextLine();

        System.out.println("请选择部门 ID:");
        int deptId = 0;
        do {
            DeptView.list();
            if (DeptView.cache == null || DeptView.cache.length == 0) {
                printError("系统还没有任何部门信息，请先创建部门信息。");
                return;
            }
            DeptVo dept = DeptView.getCacheById();
            if (dept != null) {
                deptId = dept.id;
            }
        }while (deptId == 0);

        System.out.println("请选择角色 ID:");
        int roleId = 0;
        do {
            RoleView.list();
            if (RoleView.cache == null || RoleView.cache.length == 0) {
                printError("系统还没有任何角色信息，请先创建角色信息。");
                return;
            }
            RoleVo role = RoleView.getCacheById();
            if (role != null) {
                roleId = role.id;
            }
        }while (roleId == 0);

        UserDto dto = new UserDto(username, name, mobile, email, deptId, roleId);
        Result result = userService.create(dto);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }

    public static void update() {
        UserVo user = getCacheById();
        if (user == null) {
            return;
        }

        System.out.print("请输入用户名，直接按回车表示不修改：");
        String username = MainView.input.nextLine();
        username = "".equals(username) ? user.username : username;

        System.out.print("请输入姓名，直接按回车表示不修改：");
        String name= MainView.input.nextLine();
        name = "".equals(name) ? user.name : name;

        System.out.print("请输入手机号，直接按回车表示不修改：");
        String mobile = MainView.input.nextLine();
        mobile = "".equals(mobile) ? user.mobile : mobile;

        System.out.print("请输入邮箱，直接按回车表示不修改：");
        String email= MainView.input.nextLine();
        email = "".equals(email) ? user.email : email;

        int deptId = 0;
        do {
            DeptView.list();
            if (cache == null || cache.length == 0) {
                printError("系统还没有任何部门信息，请先创建部门信息。");
                return;
            }
            DeptVo dept = DeptView.getCacheById();
            if (dept != null) {
                deptId = dept.id;
            }
        }while (deptId == 0);

        System.out.println("请选择角色 ID:");
        int roleId = 0;
        do {
            RoleView.list();
            if (RoleView.cache == null || RoleView.cache.length == 0) {
                printError("系统还没有任何角色信息，请先创建角色信息。");
                return;
            }
            RoleVo role = RoleView.getCacheById();
            if (role != null) {
                roleId = role.id;
            }
        }while (roleId == 0);

        UserDto dto = new UserDto(username, name, mobile, email, deptId, roleId);
        Result result = userService.update(dto, user.id);

        if(!result.success) {
            printError(result.message);
            return;
        }

        System.out.println(result.message);
        list();
    }

    public static void toggle() {
        UserVo user = getCacheById();
        if (user == null) {
            return;
        }
        if (user.enabled) {
            System.out.print("输入'Y'确认禁用，输入其它取消禁用：");
        } else {
            System.out.print("输入'Y'确认启用，输入其它取消启用：");
        }
        String confirm = MainView.input.nextLine();
        if (!"Y".equalsIgnoreCase(confirm)) {
            System.out.println("您已取消修改状态操作");
            return;
        }
        Result result = userService.toggle(user.id);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }

    public static UserVo getCacheById() {
        System.out.print("请输入ID， ID必须是大于0的整数：");

        if (!MainView.input.hasNextInt()) {
            MainView.input.nextLine();
            printError("ID必须为整数，请输入整数值");
            return null;
        }

        int id = MainView.input.nextInt();
        MainView.input.nextLine();

        if (id < 1) {
            printError("ID必须大于0，请重新输入");
            return null;
        }
        if (cache == null) {
            Result result = userService.list();
            cache = (UserVo[]) result.data;
        }
        UserVo user = null;
        for (UserVo item : cache) {
            if (item.id == id) {
                System.out.println("该ID对应的数据是：");
                System.out.printf("ID：%d，用户名：%s，姓名：%s，手机号：%s，邮箱：%s，部门名称：%s，角色名称：%s，状态 \n", item.id, item.username, item.name, item.mobile, item.email, item.deptId, item.roleId, item.enabled, item.deptName, item.roleName, item.enabled ? "启用" : "禁用");
                user = item;
                break;
            }
        }
        if (user == null) {
            printError("当前没有该ID对应的数据");
            return null;
        }
        return user;
    }
}
